package com.jiangfeixiang.springbootswgger2api.service.impl;

import com.jiangfeixiang.springbootswgger2api.entity.User;
import com.jiangfeixiang.springbootswgger2api.entity.UserExample;
import com.jiangfeixiang.springbootswgger2api.mapper.UserMapper;
import com.jiangfeixiang.springbootswgger2api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by jiangfeixiang on 2018/8/28
 * @author smfx1314
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    /**
     * 注入
     */
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询所有用户
     */
    @Override
    public List<User> getAllUser() {
        List<User> users = userMapper.selectByExample(null);
        return users;
    }

    /**
     * 根据id查询用户
     */
    @Cacheable(cacheNames = {"user"})
    @Override
    public User getById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        System.out.println("111111111111"+user);
        return user;
    }

    /**
     * 添加用户
     * @param user
     */
    @Override
    public void saveUser(User user) {
        userMapper.insertSelective(user);
    }

    /**
     * 校验用户名是否存在
     * @param userName
     * @return
     * 数据库没有这条记录，count==0，返回true
     */
    @Override
    public Boolean checkUserName(String userName) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria=example.createCriteria();
        criteria.andUsernameEqualTo(userName);
        long count=userMapper.countByExample(example);
        if(count==0){
            return true;
        }
        return false;
    }

    /**
     * 修改用户
     * @param user
     */
    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 根据id删除（单个）
     * @param id
     */
    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }


    /**
     * 批量删除
     * @param useridList
     */
    @Override
    public void deleteBatchUser(List<Integer> useridList) {
      /*  UserExample example=new UserExample();
        UserExample.Criteria criteria=example.createCriteria();
        criteria.andUseridIn(useridList);
        userMapper.deleteByExample(example);*/
    }
}
